package com.quectel.cms.controller.schedulingapply;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.schedulingapply.dto.SchedulingApplyOutRecordDto;
import com.quectel.core.module.schedulingapply.service.SchedulingApplyOutRecordService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/13 19:14
 */
@RestController
@RequestMapping("schedulingApplyOutRecord")
@Tag(name = "SchedulingApplyOutRecordController", description = "外出申请记录相关api")
public class SchedulingApplyOutRecordController extends BaseController {

    @DubboReference
    private SchedulingApplyOutRecordService schedulingApplyOutRecordService;

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "schedulingApplyOutRecord:list")
    @Operation(summary = "分页获取外出申请记录列表")
    public Response<Page<SchedulingApplyOutRecordDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = schedulingApplyOutRecordService.queryTotal(request.getParams());

        List<SchedulingApplyOutRecordDto> list = null;
        if (total > 0) {
            list = schedulingApplyOutRecordService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<SchedulingApplyOutRecordDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<SchedulingApplyOutRecordDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "schedulingApplyOutRecord:edit")
    @Operation(summary = "在编辑时根据id查询外出申请记录详情")
    public Response<SchedulingApplyOutRecordDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (schedulingApplyOutRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        SchedulingApplyOutRecordDto schedulingApplyOutRecordDto = schedulingApplyOutRecordService.selectById(id);

        return Response.<SchedulingApplyOutRecordDto>ok().wrap(schedulingApplyOutRecordDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "schedulingApplyOutRecord:info")
    @Operation(summary = "根据id查询外出申请记录的详情")
    public Response<SchedulingApplyOutRecordDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (schedulingApplyOutRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        SchedulingApplyOutRecordDto schedulingApplyOutRecordDto = schedulingApplyOutRecordService.selectById(id);

        return Response.<SchedulingApplyOutRecordDto>ok().wrap(schedulingApplyOutRecordDto);
    }

    /**
     * 保存
     *
     * @param schedulingApplyOutRecordDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "schedulingApplyOutRecord:edit")
    @Operation(summary = "新增外出申请记录")
    public Response<Object> save(@RequestBody @Validated SchedulingApplyOutRecordDto schedulingApplyOutRecordDto) {

        //填充基础数据
        fillSaveCommonData(schedulingApplyOutRecordDto);

        schedulingApplyOutRecordService.save(schedulingApplyOutRecordDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param schedulingApplyOutRecordDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "schedulingApplyOutRecord:edit")
    @Operation(summary = "根据id修改外出申请记录")
    public Response<Object> update(@RequestBody  @Validated SchedulingApplyOutRecordDto schedulingApplyOutRecordDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(schedulingApplyOutRecordDto.getId()));
        if (schedulingApplyOutRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(schedulingApplyOutRecordDto);

        schedulingApplyOutRecordService.updateById(schedulingApplyOutRecordDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "schedulingApplyOutRecord:delete")
    @Operation(summary = "根据多条记录id批量删除外出申请记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (schedulingApplyOutRecordService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        schedulingApplyOutRecordService.deleteBatch(ids);

        return Response.ok();
    }
}
